<%--
  Created by IntelliJ IDEA.
  User: 韩金龙
  Date: 2021/11/29
  Time: 18:13
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script src="js/jquery-1.8.2.min.js"></script>
    <script src="js/fileupload.js"></script>
    <link rel="stylesheet" href="css/css.css">
    <link rel="stylesheet" href="css/img.css">
</head>
<body>
    添加
    <form>
        商品名称:<input type="text" name="pname"><br>
        商品价格:<input type="text" name="price"><br>
        商品图片:<div class="photoUpLoad" >
        <input type="file"   class="photo" name="photo" accept="image/*"><span class="cross">+</span>
        <img src="" alt="" class="photoEcho">
        </div><br>
        仓库位置:<div id="wz"></div>
    </form>
    <button onclick="add()">添加</button>
</body>
<script>
    $(function () {
        $.ajax({
            url:"baseList.do",
            type:"post",
            dataType:"json",
            success:function(obj) {
                console.log(obj)
                for (let i in obj) {
                    $("#wz").append('<input type=checkbox name=bids value="'+obj[i].bid+'">'+obj[i].bname+'')
                }
            }
        })
    })
    function add() {
        var formData = new FormData($("form")[0]);
        var pname = $("[name='pname']").val()
        if (pname==''){
            alert("姓名不能为空")
            return false;
        }
        var price = $("[name='price']").val()
        if (price==''){
            alert("价格不能为空")
            return false;
        }
        var bids = $("[name='bids']:checked").length
        if (bids==0){
            alert("仓库不能为空")
            return false
        }

        $.ajax({
            url:"add.do",
            type:"post",
            data:formData,
            dataType:"json",
            success:function(obj) {
                if (obj>0){
                    alert("添加成功!!")
                    location="list.do";
                }
            },
            processData:false,
            contentType:false,
        })
    }
</script>
</html>
